<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var permuteUnique = function (nums) {
            // used[i - 1] === true 实在是效率太低了 树层去重 所以不推荐 很难理解
            debugger
            nums.sort((a, b) => {
                return a - b
            })
            let result = []
            let path = []
            function backtracing(used) {
                debugger
                if (path.length === nums.length) {
                    result.push([...path])
                    return
                }
                for (let i = 0; i < nums.length; i++) {
                    if (i > 0 && nums[i] === nums[i - 1] && used[i - 1] === true) {
                        continue
                    }
                    if (!used[i]) {
                        used[i] = true
                        path.push(nums[i])
                        backtracing(used)
                        path.pop()
                        used[i] = false
                    }
                }
            }
            backtracing([])
            return result
        }
        // console.log(permuteUnique([1, 1, 2]));
        console.log(permuteUnique([1, 1, 1]));
    </script>
</body>

</html>